<template>
  <div class="m-container">
    <div class="tran-box">
      <div class="c-title">
        <span>调拨后</span>
      </div>
      <hk-table
        ref="hkTable"
        :table-data="propData.selectData"
        :table-columns="tableDataList"
        :show-pag="false"
        :loading="loading"
        :rules="formRules"
        :dict-list="dictList"
        is-need-index
        :table-height="200"
        @emitData="emitData">
        <!-- 设备密级  -->
        <template #assetDepartmentId="scope">
          <treeselect
            v-model="scope.row.row.assetDepartmentId"
            :options="dictList.deptList"
            :normalizer="normalizer"
            :append-to-body="true"
            placeholder="请选择责任部门"
            @select="changeOrg" />
        </template>
        <!-- 设备密级  -->
        <template #ownerId="scope">
          <el-select
            v-model="scope.row.row.ownerId"
            placeholder="请选择"
            style="width: 100%"
            @change="changeOwn"
            @visible-change="changeVisible($event, scope.row.row)">
            <el-option v-for="item in dictList.userList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </template>
      </hk-table>
    </div>
  </div>
</template>
    
    <script>
import HkTable from '@/components/Hk-Table/index.vue'
import HkForm from '@/components/Hk-Form'
import { userList } from '@/api/specialProject/asset'
import { treeSelect } from '@/api/specialProject/personnel'
// import { queryUserList } from '@/api/assets/host.js'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import Treeselect from '@riophae/vue-treeselect'
export default {
  // eslint-disable-next-line vue/no-unused-components
  components: { HkForm, HkTable, Treeselect },
  props: {
    propData: {
      type: Object,
      default: () => {},
    },
    labelPosition: {
      type: String,
      default: 'left',
    },
  },
  data() {
    return {
      title: '新增',

      // 表单数据
      formData: {},

      dictList: {
        userList: [],
        deptList: [],
      },
      loading: false,
      tableDataList: [
        { label: '设备编号', param: 'assetCode', width: '100' },
        {
          label: '责任部门',
          param: 'assetDepartmentId',
          type: 'slot',
          list: 'deptList',
          addRedStar: (h, { column }) => {
            return [h('span', { style: 'color: red' }, '*'), h('span', ' ' + column.label)]
          },
        },
        {
          label: '责任人',
          param: 'ownerId',
          type: 'slot',
          list: 'userList',
          addRedStar: (h, { column }) => {
            return [h('span', { style: 'color: red' }, '*'), h('span', ' ' + column.label)]
          },
        },
        { label: '存放位置', param: 'storageLocation', type: 'input', maxlength: '50' },
      ],
      checkArr: ['assetDepartmentId', 'ownerId', 'storageLocation'],
      formRules: {
        assetDepartmentId: [{ required: true, message: '不能为空', trigger: 'blur' }],
        ownerId: [{ required: true, message: '不能为空', trigger: 'blur' }],
      },
      otherData: {},
      showT: true,
      id: null,
      deptId: null,
      choseColor: '',
      stagData: {},
      isClear: false,
    }
  },
  created() {
    this.getDicts()
    console.log(this.propData, 'ss')
  },
  mounted() {},
  methods: {
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children,
      }
    },

    //  周期
    // 字典获取   数组
    getDicts() {
      // 责任部门
      treeSelect().then((res) => {
        this.dictList.deptList = res
      })
    },
    changeVisible(e, node) {
      console.log(e, node, 'eee')
      if (e === true) {
        this.drawUser(node.assetDepartmentId)
      }
    },
    // 人change事件
    changeOwn(node) {
      console.log(node)
    },
    // 部门change事件
    changeOrg(node) {
      this.drawUser(node.id)
    },
    // 获取责任人list
    drawUser(id) {
      //   人
      userList({ orgId: id }).then((res) => {
        this.dictList.userList = res.map((o) => {
          o.label = o.name
          o.value = o.id
          return o
        })
      })
    },

    emitData(e) {
      this.stagData = e
    },
    // 提交事件
    async submitForm() {
      this.$refs.hkTable.subData()
      console.log(this.stagData)
      var idx = this.stagData.findIndex((item) => {
        return !item.assetDepartmentId || !item.ownerId
      })
      if (idx !== -1) {
        this.$message.error('请完善表单内容')
        return false
      }
      this.cancel()
      this.$emit('send', this.stagData)
    },
    cancel() {
      this.close()
    },
    close() {
      this.$emit('close')
    },
  },
}
</script>
 <style lang="scss" scoped>
.middle {
  .right {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
  }
}
.c-title {
  font-weight: 700;
}
#app.dark-theme {
  .m-container,
  .tab-content {
    background: transparent !important;
    color: #fff;
  }
  .tab-bg {
    background: rgba(59, 139, 255, 0.1) !important;
  }
  .sp-flex i {
    color: #4b6eef !important;
  }
  .active {
    border: 1px solid #fff !important;
    transform: scale(1.1, 1.1);
  }
}
.group-top-btn {
  display: flex;
  justify-content: flex-start;
  .group-middle {
    padding: 2px;
    margin: 2px;
    border: 1px solid transparent;
    border-radius: 6px;
    .group-btn {
      width: 30px;
      height: 30px;
      border-radius: 4px;
      cursor: pointer;
    }
  }
}
.active {
  border: 1px solid #4b6eef !important;
  transform: scale(1.1, 1.1);
}
.up-txt {
  font-size: 12px;
}
.add-row {
  margin-top: 20px;
}
.m-container {
  padding: 0;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.tran-box {
  // background: #fff;
  border-radius: 4px;
  padding: 20px;
  min-height: 400px;
  margin-bottom: 20px;
}
.el-form {
  flex: 1;
  overflow: auto;
}
footer {
  height: 70px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 20px;
}
::v-deep .el-table__body-wrapper {
  height: 350px;
}

::v-deep {
  .el-button--text {
    padding: 0;
  }

  .el-input-group__append {
    background: transparent;
  }
}
</style>
    
    